package com.masterclient.app.modules.shop.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.masterclient.app.modules.shop.beans.SellerBean;
import com.masterclient.app.widget.FoldTextView;
import com.gyf.immersionbar.ImmersionBar;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @auther hjian
 * @date 2020/4/9.
 */
public class StoreInfoLayoutAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    private Activity mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int count = 0;
    private SellerBean data;
    private String isFavorite, onSaleTitle;
    private OnInforCallback onInforCallback;

    public interface OnInforCallback {
        void favoriteClick(boolean isFavorite);
    }

    public StoreInfoLayoutAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams,
                                  SellerBean data, String isFavorite, String onSaleTitle, OnInforCallback onInforCallback) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.count = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
        this.onInforCallback = onInforCallback;
        this.isFavorite = isFavorite;
        this.onSaleTitle = onSaleTitle;
    }


    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.head_store, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        ViewTransformUtil.glideImageView(mContext, "", holder.itemView.findViewById(R.id.store_logo), new Transformation[]{new CenterCrop(),
                new RoundedCornersTransformation(ScreenUtils.dp2px(7), 0)}, R.mipmap.defaults_1);


        FoldTextView storeDesc = holder.itemView.findViewById(R.id.store_desc);
        TextView storeMoreText = holder.itemView.findViewById(R.id.store_more_text);
        ImageView storeMoreIcon = holder.itemView.findViewById(R.id.store_more_icon);
        LinearLayout storeMoreLayout = holder.itemView.findViewById(R.id.store_more_layout);
        FrameLayout storeMoreFl = holder.itemView.findViewById(R.id.store_more_fl);
        TextView storeCollect = holder.itemView.findViewById(R.id.store_collect);
        LinearLayout storeCollected = holder.itemView.findViewById(R.id.store_collected);
        ImageView storePic = holder.itemView.findViewById(R.id.store_pic);
        View storePicBg = holder.itemView.findViewById(R.id.store_pic_bg);
        ImageView storeLogo = holder.itemView.findViewById(R.id.store_logo);
        TextView storeName = holder.itemView.findViewById(R.id.store_name);
        LinearLayout storeTagRecommendll = holder.itemView.findViewById(R.id.store_tag_recommendll);
        TextView storeTagRecommend = holder.itemView.findViewById(R.id.store_tag_recommend);
        ImageView storeTagRecommendicon = holder.itemView.findViewById(R.id.store_tag_recommendicon);
        TextView storeOnsale = holder.itemView.findViewById(R.id.store_onsale);
        ImageView storeDealerIcon = holder.itemView.findViewById(R.id.store_dealer_icon);

        storePic.getLayoutParams().height = ImmersionBar.getStatusBarHeight(mContext)+ScreenUtils.dp2px(99);

        ViewTransformUtil.glideImageView(mContext, data.getLogo(), storeLogo, new Transformation[]{
                new CenterCrop(), new RoundedCornersTransformation(ScreenUtils.dp2px(7), 0)}, R.mipmap.defaults);
        storeName.setText(data.getName());
        storeCollect.setVisibility("0".equals(isFavorite) ? View.VISIBLE : View.GONE);
        storeCollected.setVisibility("0".equals(isFavorite) ? View.GONE : View.VISIBLE);

        if (!data.isIs_dealer()) {
            ViewTransformUtil.glideImageView(mContext, data.getBanner() , storePic,
                    new Transformation[]{new CenterCrop()}, R.mipmap.defaults_1);
            storeDealerIcon.setVisibility(View.GONE);
            storePicBg.setVisibility(View.VISIBLE);
        }else {
            storePic.setImageResource(R.mipmap.dealer_head_bg);
            storeCollect.setVisibility(View.GONE);
            storeCollected.setVisibility(View.GONE);
            storeDealerIcon.setVisibility(View.VISIBLE);
            storePicBg.setVisibility(View.GONE);
        }
        if (data.getShop_icon() != null && !TextUtils.isEmpty(data.getShop_icon().getBg_header_color())) {
            storeTagRecommendll.setVisibility(View.VISIBLE);
            int[] colors = {Color.parseColor(data.getShop_icon().getBg_header_color()),
                    Color.parseColor(data.getShop_icon().getBg_footer_color())};
            GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
            drawable.setCornerRadius(ScreenUtils.dp2px(2));
            drawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
            storeTagRecommendll.setBackground(drawable);
            storeTagRecommend.setText(data.getShop_icon().getTitle());
            ViewTransformUtil.glideImageView(mContext, data.getShop_icon().getPre_icon(), storeTagRecommendicon,
                    new Transformation[]{new CenterCrop()}, R.mipmap.defaults_1);
        } else {
            storeTagRecommendll.setVisibility(View.GONE);
        }

        storeOnsale.setText(onSaleTitle);


        storeDesc.setEllipText("…");
        storeDesc.setEllipColor(0xff525866);
        FoldTextView.Callback foldCallback = new FoldTextView.Callback() {
            @Override
            public void onExpand() {
                storeMoreFl.setVisibility(View.VISIBLE);
                storeMoreIcon.setImageResource(R.mipmap.search_up);
            }

            @Override
            public void onCollapse() {
                storeMoreFl.setVisibility(View.VISIBLE);
                storeMoreIcon.setImageResource(R.mipmap.search_down);
            }

            @Override
            public void onLoss() {
                storeMoreFl.setVisibility(View.GONE);
            }
        };

        if (!TextUtils.isEmpty(data.getDesc())) {
            storeDesc.setText(data.getDesc(), false, foldCallback);
            storeDesc.setVisibility(View.VISIBLE);
        } else {
            storeDesc.setText("", false, foldCallback);
            storeDesc.setVisibility(View.GONE);
        }


        storeMoreLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                storeDesc.setChanged(!storeDesc.ismExpanded());
            }
        });

        storeCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInforCallback.favoriteClick(true);
            }
        });
        storeCollected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInforCallback.favoriteClick(false);
            }
        });
    }

    public void setIsFavorite(String isFavorite) {
        this.isFavorite = isFavorite;
        notifyItemChanged(0);
    }

    public void refreshData(SellerBean data, String isFavorite, String onSaleTitle){
        this.data = data;
        this.isFavorite = isFavorite;
        this.onSaleTitle = onSaleTitle;
        notifyItemChanged(0);
    }

    @Override
    public int getItemCount() {
        return count;
    }


    @Override
    public int getItemViewType(int position) {
        return 0;
    }
}
